package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import client.CcrmApp;
import model.Envelope;
import view.MMaddTypeGui;
/**
 * MMaddTypeController is controller for add type in marketing manager
 * @author gili
 *
 */
public class MMaddTypeController extends AbstractTransfer {
	
	private MarketingManagController prevCon;
	private MMaddTypeGui addTypeGUI;
	private String Type;
	private String cat;
	private MMaddTypeController curCont;
	private ArrayList<String> category;
/**
 * 
 * @param ar is the gui of add data
 * @param lastCont is the previous controller
 */
	public MMaddTypeController(MMaddTypeGui ar,MarketingManagController lastCont){
		addTypeGUI = ar;
		prevCon = lastCont;
		curCont = this;
		addTypeGUI.addFinishActionListener(new FinishListener());
		addTypeGUI.addCancelActionListener(new CancelListener());
		initCategory();
	}
	/**
	 * initialize category combobox with data from server
	 */
	private void initCategory() { //send to server to get list of area
		//category = new ArrayList<String>();
		Envelope ev = new Envelope(null, "get Category for type");
		CcrmApp.clien.setCurrObj(curCont);
		sendToServer(ev);
	}
	
	/**
	 * 
	 *  @param message is an object received from server 
	 *  
	 */
	 public void handleDBResult(Object message) {


			 if (((Envelope)message).getTask().equals("get Category for type")){
				if (((Envelope)message).getMess().equals("found")){
					
					//draw category data from database to the combobox
					category=(ArrayList<String>)((Envelope) message).getObject();
					addTypeGUI.getComboBox().addItem("");
					for(int i = 0; i < category.size() ; i++)
						addTypeGUI.getComboBox().addItem(category.get(i));
				}
				else{
					addTypeGUI.setWarningMes("No Categories");
				}
			 }
			 else {
				 System.out.println(((Envelope)message).getMess());
					 addTypeGUI.setWarningMes(((Envelope)message).getMess());
					 addTypeGUI.ClearText();
			 }

	 }
	/**
	 * this class is a listener for "Finish" button which takes the data from the combobox and the textfield
	 * and creates new type to category in data base, if data is entered and displays warning message otherwise 
	 * @author gili
	 *
	 */
	class FinishListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			//System.out.println("finish pressed");
			Envelope ev1;
			Type = new String();
			cat = new String();
			ArrayList<String> array = new ArrayList<String>();
			String msg = new String();
			try{
				cat=(String)addTypeGUI.getComboBox().getSelectedItem();
				Type=addTypeGUI.getTypeTxt().getText();
				//System.out.println(cat+" "+Type);
				array.add(Type);
				array.add(cat);
				if(!(Type.equals("")||((Integer)(addTypeGUI.getComboBox().getSelectedIndex())).equals(0))){
					msg ="addType";
					//System.out.println("condition true");
					ev1 = new Envelope(array,msg);
					sendToServer(ev1);
					CcrmApp.clien.setCurrObj(curCont);
					}
				else
					addTypeGUI.setWarningMes("Error! Please fill out all fields");
			    }
				catch(Exception e1){
					e1.printStackTrace(); 
				}
		}
		
	}
	/**
	 * This class is a listener for the "cancel" button
	 * It implements interface ActionListener 
	 * disposes current gui and displays previous gui	
	 * @author gili
	 *
	 */
	class CancelListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			addTypeGUI.dispose();
			prevCon.getMarkManGui().setVisible(true);
		}
	}

}
